package com.dykj.fireFighting.server.base.effecttimesetting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dykj.fireFighting.api.base.effecttimesetting.entity.EffectiveTimeSetting;
import com.dykj.fireFighting.api.base.effecttimesetting.service.IEffectiveTimeSettingService;
import com.dykj.fireFighting.api.base.effecttimesetting.vo.EffectiveTimeSettingVo;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.server.base.effecttimesetting.mapper.EffectiveTimeSettingMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;

/**
 * 有效时间设置
 *
 * @Author zly
 * @Date 2021-08-12 14:35:29
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class EffectiveTimeSettingServiceImpl extends BaseServiceImpl<EffectiveTimeSettingMapper, EffectiveTimeSetting> implements IEffectiveTimeSettingService {


    @Override
    public EffectiveTimeSettingVo isEffective(Date sourceTime, String type,String enterpriseId) {
        QueryWrapper<EffectiveTimeSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0);
        queryWrapper.eq("unit_id",enterpriseId);
        queryWrapper.orderByDesc("create_time");
        EffectiveTimeSetting effectiveTimeSetting = this.baseMapper.selectOne(queryWrapper);
        EffectiveTimeSettingVo effectiveTimeSettingVo = new EffectiveTimeSettingVo();
        if(null == effectiveTimeSetting){
            effectiveTimeSettingVo.setIsEffective(false);
            return effectiveTimeSettingVo;
        }
        int time = 0;
        //当前时间
        LocalDateTime nowTime = LocalDateTime.now();
        //源时间
        Instant instant = sourceTime.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime parse = LocalDateTime.ofInstant(instant, zone);

        //隐患（维修完成）整改有效时间
        if ("hiddenDanger".equals(type)) {
            time = effectiveTimeSetting.getHiddenDangerTime();
            //火警有效复核率的时间
        } else if ("fireAlarm".equals(type)) {
            time = effectiveTimeSetting.getFireAlarmTime();
            //故障有效恢复率的时间
        } else if ("fault".equals(type)) {
            time = effectiveTimeSetting.getFaultTime();
            //值班记录上传有效时间
        } else if ("duty".equals(type)) {
            time = effectiveTimeSetting.getDutyTime();
        } else if("patrol".equals(type)){
            time = effectiveTimeSetting.getPatrolTime();
        }
        parse=parse.plusMinutes(time);

        //EffectiveTimeSettingVo effectiveTimeSettingVo = new EffectiveTimeSettingVo();
        effectiveTimeSettingVo.setIsEffective(nowTime.isBefore(parse));
        effectiveTimeSettingVo.setTime(time);
        return effectiveTimeSettingVo;
    }

    @Override
    public PageVo<EffectiveTimeSetting> queryList(CommonDto commonDto, HashMap<String, Object> params) {

        Page page = new Page();
        // 设置当前页码
        page.setCurrent(commonDto.getPage());
        // 设置页大小
        page.setSize(commonDto.getLimit());

        return new PageVo<EffectiveTimeSetting>(this.baseMapper.queryList(page,params));
    }

    @Override
    public Long selectByUnintId(String enterpriseId) {
        QueryWrapper<EffectiveTimeSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0);
        queryWrapper.eq("unit_id",enterpriseId);
        return this.baseMapper.selectCount(queryWrapper);
    }
}
